/****************************************************************************
 @title	  	 	AVR-FFT auf ATmega128
 @author    	Matthias Born <matthias_born@web.de>
 @file   	 	uart.c
 @target 	   	ATmega128
*****************************************************************************/
#include "uart.h"
#include "ks0108.h"

/**
* @brief	Ausgeben einzelenr Buchstaben
* @return	0
*/
int uart_putc(unsigned char c)
{
    while (!(UCSR0A & (1<<UDRE)))  /* warten bis Senden moeglich */
    {
    }

    UDR0 = c;                      /* sende Zeichen */
    return 0;
}

/**
* @brief	Ausgeben von Strings. puts ist unabhaengig vom Controllertyp.
* @return	nothing
*/
void uart_puts (char *s)
{
    while (*s)
    {   /* so lange *s != '\0' also ungleich dem "String-Endezeichen" */
        uart_putc(*s);
        s++;
    }
}
/**
* @brief	Senden des Char war erfolgreich
* @return	nothing
*/
void sentchar1 (unsigned char datasent)
{
//	while (!( UCSR1A & (1<<UDRE)));
	while (!( UCSR0A & (1<<UDRE)));

//	UDR1 = datasent;	
	UDR0 = datasent;

}

void printPRGstr1 (const char *str)
{
	unsigned char tmpchar;

	tmpchar = pgm_read_byte(str++);
	while (tmpchar!='\0')
	{
		sentchar1(tmpchar);
		tmpchar = pgm_read_byte(str++);
	}
}

/**
* @brief	Initialisiert die UART-Kommunikation
* @return	nothing
*/

void uart_initial (void)
{
	unsigned int bauddiv;
		
	/* Set baud rate */
	bauddiv = fosc/(baudrate*16L)-1;
	
	//UBRR1H = (unsigned char)(bauddiv>>8);
	UBRR0H = (unsigned char)(bauddiv>>8);

	//UBRR1L = (unsigned char)bauddiv;
	UBRR0L = (unsigned char)bauddiv;
	
	/* Enable RxD/TxD and interrupts of USART1*/
	//UCSR1B = (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1);//|(1<<TXCIE1);
	UCSR0B = (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1);//|(1<<TXCIE1);
	
	// enable interrupts
	sei();
	
	print("UART0: 57600 bps @ 8n1\r\n");
}

/**
* @brief	Interrupt-Vector des UART
* @return	nothing
*/
SIGNAL(SIG_UART0_RECV)
{
	unsigned char charrev;
	
//	charrev = UDR1;
	charrev = UDR0;

	/*switch (charrev)
	{
		case 'l':	// LCD backlight
			{
				//BKLIGHT;
			}break;
		default:
			{
				;
			}
	}*/
}
